package com.springboot.redis.demo.bitmap;

import com.springboot.redis.demo.RedisClient;

/**
 * 网站用户操作日志统计案例
 *
 * @author: zr
 * @date: 2021-11-17
 */
public class BitMapDemo extends RedisClient {

    /**
     * 获取Redis Key
     *
     * @param operation
     * @return {@link String}
     * @throws
     * @date 2021-11-17
     */
    public static String getKey(String operation) {
        String currentDate = getCurrentDate();
        return "operation::" + operation + "::" + currentDate + "::log";
    }

    /**
     * 记录某种操作 被 某个用户执行了
     *
     * @param operation
     * @param userId
     * @return
     * @throws
     * @date 2021-11-17
     */
    public static void recordUserOperationLog(String operation, long userId) {
        jedis.setbit(getKey(operation), userId, String.valueOf(1));
    }

    /**
     * 判断用户今天是否执行了这个操作
     *
     * @param operation
     * @param userId
     * @return {@link boolean}
     * @throws
     * @date 2021-11-17
     */
    public static boolean hasOperated(String operation, long userId) {
        return jedis.getbit(getKey(operation), userId);
    }

    public static void main(String[] args) {
        long userId = 100L;
        String operation = "操作1";
        recordUserOperationLog(operation, userId);

        System.out.println("用户" + userId + "是否执行过 " + operation + ":" + (hasOperated(operation, userId) ? "是" : "否"));

        long userId2 = 200;
        System.out.println("用户" + userId2 + "是否执行过 " + operation + ":" + (hasOperated(operation, userId2) ? "是" : "否"));

    }
}
